//	ID : pallab81
// 	PROG : 11770
// 	LANG : C++
// 	2011-07-08-16.45.06 Friday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;


#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second

int seen[(int)1e4+100 ],seen2[ (int)1e4+100 ];
vector< vector<int> > p ; //path
int N,E;

inline void read(){
    scanf("%d %d",&N,&E);
    p.assign(N+5, vector<int>() );

    for(int i=1,x,y;i<=E; ++i){
        scanf("%d %d",&x,&y);
        p[x].pb(y);
    }

}

inline void dfs1(int now_node, stack<int> &nodes){
    if( seen[now_node]==-1 ){
        seen[now_node]=0;
        fo(i,SZ( p[now_node] )){
            dfs1( p[now_node][i], nodes);
        }
        nodes.push( now_node );
    }
}
inline void dfs2(int now_node){
    if( seen2[now_node]==-1 ){
        seen2[now_node]=0;
        fo(i,SZ( p[now_node])){
            dfs2(p[now_node][i]);
        }
    }
}

inline void doit(int cases){
    read();


    memset( seen,-1,sizeof(seen) );
    stack<int> nodes;
    for(int i=1;i<=N;++i){
        if( seen[i]==-1 ){
            dfs1(i,nodes);
        }
    }
    int res=0;
    memset( seen2,-1,sizeof(seen2));
    for(int i; nodes.empty()==false ; nodes.pop()){
        i = nodes.top();
        if( seen2[i]==-1){
            ++res;
            dfs2(i);
        }
    }
    printf("Case %d: %d\n",cases,res);
}

int main(){
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i){
        doit(i);
    }

return 0;
}

